package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ArtilceIsDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;
    @Autowired
    private ArticleService articleService;


//    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
//    public void onMessage(String message){
//        if(StringUtils.isNotBlank(message)){
//            Map map = JSON.parseObject(message, Map.class);
//            apArticleConfigService.updateByMap(map);
//            log.info("article端文章配置修改，articleId={}",map.get("articleId"));
//        }
//    }

    /**
     * 阅读数据库更新
     * @param message
     */
    @KafkaListener(topics = WmNewsMessageConstants.READ_BEHAVIOR)
    public void read(String message){
        articleService.upDateRead(message);
    }

    /**
     * 点赞数据库更新
     */
    @KafkaListener(topics = WmNewsMessageConstants.LIKES_BEHAVIOR)
    public void likes(String message){

        Map<String,Object> map = JSON.parseObject(message, Map.class);
        articleService.upDateLike(map);
    }

    /**
     * 收藏数据库更新
     */
    @KafkaListener(topics = WmNewsMessageConstants.COLLECTION_BEHAVIOR)
    public void collection(String message){

        Map<String,Object> map = JSON.parseObject(message, Map.class);
        articleService.upDatecollection(map);
    }


    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMessage(String mess){
        if(StringUtils.isNotBlank(mess)){
            ArticleVisitStreamMess articleVisitStreamMess = JSON.parseObject(mess, ArticleVisitStreamMess.class);
            articleService.updateScore(articleVisitStreamMess);
        }
    }
}
